import java.awt.Color;
import se.lth.cs.ptdc.images.ImageFilter;

/** IdentityFilter beskriver en identitetstransformation */
public class SobelFilter extends ImageFilter {
	private static short[][] X_SOBEL = { {-1, 0, 1},{-2, 0, 2},{-1, 0, 1}};
	private static short[][] Y_SOBEL = { {-1, -2, -1},{0, 0, 0},{1, 2, 1}};
	
	/** skapa ett filterobjekt med namnet name */
	public SobelFilter(String name) {
		super(name);
	}


	/**
	 * filtrera bilden i matrisen inPixels och returnera resultatet i en ny
	 * matris. Utnyttja eventuellt värdet av parameter
	 */
	public Color[][] apply(Color[][] inPixels, double paramValue) {
		int height = inPixels.length;
		int width = inPixels[0].length;
		Color[][] outPixels = new Color[height][width];

		short[][] intensity = computeIntensity(inPixels);
		short sx = 0;
		short sy = 0;
		
		for(int i = 0; i < intensity.length; i++ ) {
			for(int j = 0; j < intensity[0].length; j++) {
				if(i > 0 && i < height - 1 && j > 0 && j < width -1) {
				sx = convolve(intensity, i, j, X_SOBEL, 1);
				sy = convolve(intensity, i, j, Y_SOBEL, 1);
				
				}
				if((Math.abs(sx) + Math.abs(sy)) > paramValue ) {
					outPixels[i][j] = Color.BLACK; 
				}else {
					outPixels[i][j] = Color.WHITE;
				}
			}
		}
		return outPixels;
	}
}